﻿using Heima.OA.Common;
using Heima.OA.IBLL;
using Heima.OA.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Heima.OA.UI.Portal.Controllers
{
    public class RoleInfoController : BaseController
    {
        public IRoleInfoService RoleInfoService { get; set; }
        // GET: RoleInfo
        public string Index(PaginationQuery paginationQuery)
        {
            //var roleList = RoleInfoService.GetEntities(u => u.DelFlag == delflagNormal).AsEnumerable();
            int total = 0;
            var roleList = RoleInfoService.GetPageEntities(paginationQuery.pagesize, paginationQuery.pageindex, out total, u => u.DelFlag == delflagNormal,
                s => s.SubTime, true).AsQueryable();

            PaginationData data = new PaginationData() { total = total, rows = JsonHandler.ToJson(roleList) };
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(data));

            return JsonHandler.ToJson(msg);
        }

        #region 获取角色列表
        public string GetList()
        {
            var roleList = RoleInfoService.GetEntities(u => u.DelFlag == delflagNormal).AsEnumerable();
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(roleList));
            return JsonHandler.ToJson(msg);
        }
        #endregion
        #region 添加
        public string Create(RoleInfo roleInfo)
        {
            roleInfo.DelFlag = (short)Heima.OA.Model.DelFlagEnum.Normal;
            roleInfo.ModifiedOn = CDateTime.GetDefaultDateTime();
            roleInfo.SubTime = DateTime.Now;

            RoleInfoService.Add(roleInfo);
            JsonMessage message = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "添加成功!");
            return JsonHandler.ToJson(message);
        }
        #endregion
        #region 编辑
        public string Edit(RoleInfo roleInfo)
        {
            RoleInfo editRoleInfo = RoleInfoService.GetModel(roleInfo.Id);
            string res = string.Empty;
            if (editRoleInfo == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "编辑错误!"));
            }
            else
            {
                editRoleInfo.RoleName = roleInfo.RoleName;
                editRoleInfo.Remark = roleInfo.Remark;
                editRoleInfo.ModifiedOn = DateTime.Now;
                bool success = RoleInfoService.Update(editRoleInfo);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "编辑成功!"));
                }
            }

            return res;
        }
        #endregion
        #region 删除
        public string Delete(int id)
        {
            RoleInfo roleInfo = RoleInfoService.GetModel(id);
            string res = string.Empty;
            if (roleInfo == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "删除错误!"));
            }
            else
            {
                roleInfo.DelFlag = (int)DelFlagEnum.Deleted;
                bool success = RoleInfoService.Update(roleInfo);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "删除成功!"));
                }
            }
            return res;
        }
        #endregion

    }
}